﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Web.Mvc;
using System.Collections.Generic;

namespace Bookmark.Net.Models
{
    public partial class Bookmark_NetDataContext
    {

        public List<User> GetUsers()
        {
            return Users.ToList();
        }
        //public List<User> GetUsersByLastName(string lastName)
        //{
        //    return Users.Where(u => u.LastName == lastName).ToList();
        //}
        public User GetUserById(int id)
        {
            return Users.Single(u => u.UserId == id);
        }

        public PagedList<Item> GetItems(int? page)
        {
            
            //return Items.ToPagedList(ToPagedList(page ?? 0));
            return (from i in Items
                              orderby i.CreateDate
                              select i).ToPagedList(page ?? 0); // if no page specified then use 0
        }

            //viewData.Items = (from p in db.Products
            //                  where p.Color == "Red"
            //                  orderby p.Name
            //                  select p).ToPagedList(page ?? 0); // if no page specified then use 0
        public PagedList<Item> GetItemsByUser(int? page, string userName)
        {
            //int userId = Users.Single(u => u.UserName = userName).UserId;
            //return (from i in Items
            //          where i.UserId == userId
            //          orderby i.CreateDate
            //          select i).ToPagedList(page ?? 0); // if no page specified then use 0

            return (from u in Users
                    from i in u.Items
                    where u.UserName == userName
                    select i).ToPagedList(page ?? 0); // if no page specified then use 0
        }
        public PagedList<Item> GetItemsByTag(int? page, string tagName)
        {
            return (from t in Tags
                    from it in t.ItemTags
                    where t.TagName == tagName
                    orderby it.Item.CreateDate  // not sure how well this will perform
                    select it.Item).ToPagedList(page ?? 0); // if no page specified then use 0
        }
        public PagedList<Item> GetItemsByUserTag(int? page, string userName, string tagName)
        {
            return (from u in Users
                    from t in u.Tags
                    from it in t.ItemTags
                    where u.UserName == userName
                    where t.TagName == tagName
                    orderby it.Item.CreateDate  // not sure how well this will perform
                    select it.Item).ToPagedList(page ?? 0); // if no page specified then use 0
        }
        public List<Tag> GetRelatedTags(string tagName)
        {
            // TODO: implement
            return null;
        }
        public List<Tag> GetRelatedTagsOfUser(string tagName, string userName)
        {
            // TODO: implement
            return null;
        }

    }
}
